﻿using System;
using System.Collections.Generic;
using System.Data;
using SAS.Core.Result;

namespace SAS.DataAccess
{
    public abstract class BaseDAO
    {
        /// <summary>
        /// Get the token key
        /// </summary>
        public string TokenKey
        {
            get; protected set;
        }

        protected BaseDAO(){}

        protected BaseDAO(string tokenKey)
        {
            TokenKey = tokenKey;
        }

        protected SASResult<IList<string>> GetDropDownList(string column, Func<IDataReader, string> make)
        {
            //Indentify SQL store procedure name
            const string storeProcedureName = "[scmAdminApp].[uspDropDown_GetByToken$ColumnName]";

            //Build SQL parameters and their values.
            Parameter[] sqlParameters = new[]
                                        {
                                            new Parameter {Name = "@Token", Value = TokenKey},
                                            new Parameter {Name = "@ColumnName", Value = column}
                                        };

            return DB.ReadList(storeProcedureName, make, sqlParameters);
        }

        protected SASResult<IList<T>> Search<T>(int pagenumber, int pagesize, string orderby, string conditionList,
                                                string viewName, Func<IDataReader, T> make)
        {
            //Indentify SQL store procedure name
            const string storeProcedureName = "[scmAdminApp].[uspGrid_GetPageByToken$ViewName]";

            //Build SQL parameters and their values.
            Parameter[] sqlParameters = new[]
                                            {
                                                new Parameter{Name = "@Token", Value = TokenKey},
                                                new Parameter{Name = "@ViewName", Value = viewName},
                                                new Parameter{Name = "@PageNum", Value = pagenumber},
                                                new Parameter{Name = "@PageSize", Value = pagesize},
                                                new Parameter{Name = "@OrderByCol", Value = orderby},
                                                new Parameter{Name = "@ConditionList", Value = conditionList},
                                                new Parameter
                                                    {
                                                        Name = "@RecordCount",
                                                        DbType = SqlDbType.Int,
                                                        ParameterDirection = ParameterDirection.Output,
                                                        OutputReturnName = "RecordCount",
                                                        Value = 0,
                                                    }
                                            };

            return DB.ReadList(storeProcedureName, make, sqlParameters);
        } 
    }
}